<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/5 0005
 * Time: 14:25
 */

namespace app\home\controller;


use app\model\model\Ad;
use app\model\model\Application;
use app\model\model\Bujian;
use app\model\model\Parts;
use app\model\model\Product_type;
use app\model\model\Seo;
use think\Controller;
use think\Request;
use think\Url;

class Compare extends Base
{
    protected $_Seo;
    protected $_Ad;
    protected $_product;
    protected $_Parts;
    protected $_sample;
    protected $_News;
    protected $_app;
    protected $_Producttype;
    protected $bujian;
    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->_product = new \app\model\model\Compare();
        $this->_sample = new \app\model\model\Sample();
        $this->_News = new \app\model\model\News();
        $this->_Parts = new Parts();
        $this->_Ad = new Ad();
        $this->_Seo = new Seo();
        $this->_app = new Application();
        $this->_Producttype = new Product_type();
        $this->bujian = new Bujian();
    }
    public function index(){
        $u = $_SERVER['REQUEST_URI'];
	/*
        if(strpos($u,'home')!==false){
            $this->redirect('/Product-Category.html',301);
        }
        if(strpos($u,'product.html')!==false){
            $this->redirect('/Product-Category.html',301);
        }
        if(strpos($u,'html')==false){
            $this->redirect('/Product-Category.html',301);
        }
		*/
        //SEO
        $seo = $this->_Seo->getOne(['id'=>1]);
        $this->assign('seo',$seo);
        //banner
        $banner = $this->_Ad->getListHome(['type_id'=>14,'flag'=>1]);
        $this->assign('banner',$banner);
        $list = $this->_product->getListSelect(['flag'=>1],true);
        foreach ($list as $k => $v){
            $v['uname'] = str_replace(" ","-",$v['name']);
            $v['params'] = json_decode($v['params'],true);
            $v['cover'] = json_decode($v['cover'],true);
        }
        $type1 = $this->_Producttype->getOne(7);
        $type2 = $this->_Producttype->getOne(6);
        $type3 = $this->_Producttype->getOne(5);
        $type = [
            'type1'=>$type1,
            'type2'=>$type2,
            'type3'=>$type3,
        ];
        $this->assign('type',$type);
        $index = $this->_Producttype->getOne(12);
        $this->assign('index',$index);
        $this->assign('list',$list);
        //dump($list[0]['cover'][0]);
        //exit;
        return $this->fetch();
    }
    public function producttype(){
        $id = input('id');
        $data = $this->_Producttype->getOne($id);
        if($data === null||$id==12){
            $this->error('没有分类数据');
        }
        //SEO
        $seo = $this->_Seo->getOne(['id'=>1]);
        $this->assign('seo',$seo);
        //banner
        $banner = $this->_Ad->getListHome(['type_id'=>14,'flag'=>1]);
        $this->assign('banner',$banner);

        $list = $this->_product->getListSelect(['flag'=>1,'type_id'=>$id],true);

        foreach ($list as $v){
            $v['uname'] = str_replace(" ","-",$v['name']);
            $v['img'] = json_decode($v['img'],true);
            $v['params'] = json_decode($v['params'],true);
        }

        $this->assign('list',$list);


        $rankdata = $this->_Producttype->getOne($data['rank']);
        if($rankdata != null){
            $rankdata['uname'] = str_replace(" ","-",$rankdata['name']);
        }


        $this->assign('rank',$rankdata);

        $this->assign('data',$data);
        return $this->fetch();
    }
    public function view(){

        $id = input('name');
    //    $id = $id[1];
        $data = $this->_product->getOneByName(['id'=>$id]);
        if($data === null){
		$this->error('没有产品数据');
	}
	$type = $this->_Producttype->getOne($data['type_id']);

        $this->assign('type',$type);
        $this->_product->up(['id'=>$data['id']],['view_num'=>($data['view_num']+1)]);
        //产品图片
        $data['img'] = json_decode($data['img'],true);
        $data['cover'] = json_decode($data['cover'], true);
        //产品参数
        $data['params_mm'] = json_decode($data['params_mm'],true);
        $data['params_inch'] = json_decode($data['params_inch'],true);
        //部件列表
        $data['parts'] = json_decode($data['parts'],true);
        $pid = implode(',',$data['parts']);
        $parts = $this->_Parts->getListSelect(['id'=>['in',$pid]],true);
        foreach ($parts as $v){
            $v['uname'] = str_replace(" ","-",$v['name']);
        }
        $data['parts'] = $parts;
        //3D图
        //$data['dimensional'] = json_decode($data['dimensional'],true);
        //中间那个图
        $data['handle'] = json_decode($data['handle'],true);
        //样品列表
        $data['sample'] = json_decode($data['sample'],true);
        $sid = implode(',',$data['sample']);
        $data['sample'] = $this->_sample->getListSelect(['id'=>['in',$sid]],true);
        //知识列表
        $data['lore'] = json_decode($data['lore'],true);
        $eid = implode(',',$data['lore']);
        $data['lore'] = $this->_News->getListSelect(['id'=>['in',$eid]],true);
        foreach ($data['lore'] as $v){
            $v['uname'] = str_replace(" ","-",$v['name']);
        }
        //部件列表
        $data['bujian'] = json_decode($data['bujian'],true);
        $eid = implode(',',$data['bujian']);
        $data['bujian'] = $this->bujian->getListSelect(['id'=>['in',$eid]],true);
        foreach ($data['bujian'] as $v){
            $v['uname'] = str_replace(" ","-",$v['name']);
        }
        //应用列表
        $data['application'] = json_decode($data['application'],true);
        $eid = implode(',',$data['application']);
        $data['application'] = $this->_app->getListSelect(['id'=>['in',$eid]],true);
        foreach ($data['application'] as $v){
            $v['uname'] = str_replace(" ","-",$v['name']);
            $v['img_url'] = json_decode($v['img_url'],true);
        }
        //推荐产品
        $data['product'] = json_decode($data['product'],true);
        $eid = implode(',',$data['product']);
        $data['product'] = $this->_product->getListSelect(['id'=>['in',$eid]],true);
        $flag = 0;
        foreach ($data['product'] as $k=>$v){
            $v['uname'] = str_replace(" ","-",$v['name']);
            $v['img'] = json_decode($v['img'],true);
            $flag = $k;
        }
        $this->assign('flag',$flag+1);
        //技术参数
        $data['technical_mm_data'] = json_decode($data['technical_mm_data'],true);
        $data['technical_mm_rows'] = json_decode($data['technical_mm_rows'],true);
        $data['technical_mm_header'] = json_decode($data['technical_mm_header'],true);
        $data['technical_inch_data'] = json_decode($data['technical_inch_data'],true);
        $data['technical_inch_rows'] = json_decode($data['technical_inch_rows'],true);
        $data['technical_inch_header'] = json_decode($data['technical_inch_header'],true);

        $this->assign('data',$data);
        return $this->fetch();
    }
    /*获取3D图列表*/
    public function ajaximg(){
        $name = str_replace("-"," ",input('name'));
        $data = $this->_product->getOneByName(['name'=>$name]);
        $data['dimensional'] = json_decode($data['dimensional'],true);
        return json_echo_msg(200,'aa',$data['dimensional']);
    }

}
